package com.ruyuan.rapid.core;

import com.lmax.disruptor.*;
import com.ruyuan.rapid.common.constants.BasicConst;
import com.ruyuan.rapid.common.constants.RapidBufferHelper;
import com.ruyuan.rapid.common.util.NetUtils;
import lombok.Data;

/**
 * 网关通用配置信息
 * @author mac
 */
@Data
public class RapidConfig {

    /**
     * 网关的默认端口
     */
    private int port = 8888;
    /**
     * 网关服务唯一id
     */
    private String rapidId = NetUtils.getLocalIp() + BasicConst.COLON_SEPARATOR + port;
    /**
     * 网关的注册中心地址
     */
    private String registryAddress = "http://192.168.11.114:2379,http://192.168.11.115:2379,http://192.168.11.116:2379";
    /**
     * 网关的命名空间: dev test prod
     */
    private String namespace = "rapid";
    /**
     * 环境参数
     */
    private String env = "dev";
    /**
     * 网关服务器的CUP核数映射的线程数
     */
    private int processThread = Runtime.getRuntime().availableProcessors();
    /**
     * Netty的Boss线程数
     */
    private int eventLoopGroupBossNum = 1;
    /**
     * Netty的Worker线程数
     */
    private int eventLoopGroupWorkNum = processThread;
    /**
     * 是否开启epoll
     */
    private boolean useEPoll = true;
    /**
     * 是否开启Netty的内存分配机制
     */
    private boolean nettyAllocator = true;
    /**
     * http body报文最大大小
     */
    private int maxContentLength = 64 * 1024 * 1024;
    /**
     * httpAsync 参数选项
     */
    // TODO

    /**
     * dubbo开启连接数数量
     */
    private int dubboConnections = processThread;
    /**
     * completableFuture回调处理结果: whenComplete, whenCompleteAsync
     * 默认是单异步模式,false代表双异步
     */
    private boolean whenComplete = true;
    /**
     * 网关队列配置:缓冲区模式
     */
    private String bufferType = RapidBufferHelper.FLUSHER;
    /**
     * 网关队列配置:队列大小
     */
    private int bufferSize = 16 * 1024;
    /**
     * 网关队列配置:阻塞/等待策略
     */
    private String waitStrategy = "blocking";

    //	默认请求超时时间 3s
    private long requestTimeout = 3000;

    //	默认路由转发的慢调用时间 2s
    private long routeTimeout = 2000;

    //	kafka地址
    private String kafkaAddress = "";//"192.168.11.51:9092";

    //	网关服务指标消息主题
    private String metricTopic = "rapid-metric-topic";

    public WaitStrategy getActualWaitStrategy() {
        switch (waitStrategy) {
            case "blocking":
                return new BlockingWaitStrategy();
            case "busySpin":
                return new BusySpinWaitStrategy();
            case "yield":
                return new YieldingWaitStrategy();
            case "sleeping":
                return new SleepingWaitStrategy();
            default:
                return new BlockingWaitStrategy();
        }
    }

    /**
     * Http async client选项
     * http连接超时时间
     */
    private int httpConnectionTimeout = 30000;
    /**
     * 客户端请求超时时间
     */
    private int httpRequestTimeout = 30000;
    /**
     * 客户端最大重试次数
     */
    private int httpMaxRequestRetry = 2;
    /**
     * 客户端请求最大连接数
     */
    private int httpMaxConnections = 10000;
    /**
     * 客户端每个地址支持的最大连接数
     */
    private int httpConnectionPerHost;
    /**
     * 客户端空闲连接超时时间,默认60秒
     */
    private int httpPooledConnectionIdleTimeout = 60 * 1000;

//                .setConnectTimeout(1000)
//                .setRequestTimeout(2000)
//                .setAllocator(PooledByteBufAllocator.DEFAULT)
//                .setCompressionEnforced(true)
//                .setMaxConnections(3000)
//                .setMaxConnectionsPerHost(1000)
//                .setPooledConnectionIdleTimeout(5000);
}
